using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using Microsoft.Practices.RecipeFramework.Library.Editors;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Assets.Helpers.KeyRegistration;
using System;
using VSSDKAssist.Enums;
using VSSDKAssist.Assets.Helpers;
using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;

namespace VSSDKAssist.Recipes.ConfigureVSPlk
{
	/// <summary>
	/// Custom page for ConfigureVSPlk recipe
	/// </summary>
	public partial class ConfigureVSPlkCP2 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigureVSPlkCP2"/> class.
		/// </summary>
		public ConfigureVSPlkCP2()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigureVSPlkCP2"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public ConfigureVSPlkCP2(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the RESX project item.
		/// </summary>
		/// <value>The RESX project item.</value>
		[RecipeArgument]
		public ProjectItem ResxProjectItem
		{
			get { return (ProjectItem)veResxProjectItem.Value; }
			set
			{
				if(value != null)
				{
					veResxProjectItem.TextBox.Text = new ProjectItemConverter().ConvertTo(value, typeof(string)) as string;
				}
			}
		}

		/// <summary>
		/// Gets or sets the Plk.
		/// </summary>
		/// <value>The Plk.</value>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value"), RecipeArgument]
		public string Plk
		{
			get { return (string)vePLK.Value; }
			set
			{
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veResxProjectItem.BeginInit();
					this.veResxProjectItem.ValueType = typeof(ProjectItem);
					base.Site.Container.Add(veResxProjectItem);
					SolutionPickerEditor solutionPickerEditor = new SolutionPickerEditor();
					StringDictionary solutionPickerEditorAttributes = new StringDictionary();
					solutionPickerEditorAttributes.Add("UnboundReferenceType", "VSSDKAssist.Recipes.Common.References.ResxProjectItemReference, VSSDKAssist");
					solutionPickerEditor.Configure(solutionPickerEditorAttributes);
					this.veResxProjectItem.EditorInstance = solutionPickerEditor;
					this.veResxProjectItem.ConverterInstance = new ProjectItemConverter();
					this.veResxProjectItem.EndInit();

					this.vePLK.BeginInit();
					this.vePLK.ValueType = typeof(string);
					base.Site.Container.Add(vePLK);
					this.vePLK.EndInit();
				}
			}
		}

		private void vePLK_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string plk = vePLK.Value as string;
			DictionaryService.SetValue("Plk", plk);
		}

		private void veResxProjectItem_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			ProjectItem resxProjectItem = (ProjectItem)veResxProjectItem.Value;
			DictionaryService.SetValue("ResxProjectItem", resxProjectItem);
		}

		private void lnkVSIP_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start(Properties.Resources.VSIPUrl);
		}

		private void btnGetPLK_Click(object sender, System.EventArgs e)
		{
			string companyName = DictionaryService.GetValue("PkgCompanyName").ToString();
			string packageName = DictionaryService.GetValue("PackageName").ToString();
			Type packageType = DictionaryService.GetValue("VSPackageType") as Type;
			Guid packageGuid = packageType.GUID;
			string packageVersion = DictionaryService.GetValue("PackageVersion").ToString();
			string vsEdition = DictionaryService.GetValue("VSEdition").ToString();

			MinimumProductEdition minProductEdition;

			if(vsEdition.Equals("Standard"))
			{
				minProductEdition = MinimumProductEdition.Standard;
			}
			else 
			{
				minProductEdition =MinimumProductEdition.Professional;
			}

			if(MessageBox.Show(
				Properties.Resources.PLKMessage, 
				Properties.Resources.InformationMessage, 
				MessageBoxButtons.OKCancel) == DialogResult.OK)
			{
				PLKRegistrator registrator = 
					new PLKRegistrator(
						companyName, 
						packageName, 
						packageGuid, 
						packageVersion,
						minProductEdition, 
						MinimumVSVersion.VisualStudio2008);

				IVsUIShell uishell = this.GetService(typeof(IVsUIShell)) as IVsUIShell;

				uishell.SetWaitCursor();
				
				string plk = registrator.Register();
				
				this.vePLK.TextBox.Text = plk;
				DictionaryService.SetValue("Plk", plk);

				uishell.SetWaitCursor();
			}
		}
	}
}